<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog">
			<el-transfer
				v-model="value"
				filterable
				filter-placeholder="请输入学生姓名"
				:data="state.data"
				:titles="['未选中', '已选中']"
				class="group-dialog-transfer"
				:button-texts="['取消选中', '选中学生']"
				@change="dataChange"
				v-loading="state.loading"
			/>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="studentGroupDialog">
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { experimentInfo, studentInfo } from '/@/api/student/group';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['student']);

// 定义父组件传过来的值
const value = ref([]);
const props = defineProps({
	dialog: Object,
});

// 定义变量内容
const state = reactive({
	ruleForm: {
		teacherId: '',
		teacherName: '',
		phone: '',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	data: [],
	loading: false,
});

const student = ref<any[]>([]);

// 打开弹窗
const openDialog = (params: any) => {
	state.dialog.title = '新增学生';
	state.dialog.submitTxt = '新 增';
	state.dialog.isShowDialog = true;
	state.loading = true;
	value.value = params.length === 0 ? [] : params;
	experimentInfo(props.dialog.id)
		.then((res) => {
			if (res.code === '2000') {
				studentInfo(res.data[0].lesson_id)
					.then((result) => {
						if (result.code === '2000') {
							state.data = result.data.map((item: any) => {
								return {
									label: item.student_name,
									key: item.student_id,
								};
							});
							state.loading = false;
						} else {
							ElMessage.error('获取学生信息失败!');
							state.loading = false;
						}
					})
					.catch((err) => {
						ElMessage.error('获取学生信息失败!' + err);
						state.loading = false;
					});
			} else {
				ElMessage.error('获取实验信息失败!');
				state.loading = false;
			}
		})
		.catch((err: any) => {
			ElMessage.error('获取实验信息失败!' + err);
			state.loading = false;
			emit('student', 'error');
		});
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
//数据改变
const dataChange = () => {
	
};
// 提交
const onSubmit = () => {
	closeDialog();
	emit('student', value.value);
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
.group-dialog-transfer {
	margin-left: 2%;
}
</style>
